package org.example.embeddedPgsql.pgsqlembeddeddemo

import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.junit4.SpringRunner
import ru.yandex.qatools.embed.postgresql.EmbeddedPostgres

import java.sql.Connection
import java.sql.DriverManager
import java.sql.Statement

//import static org.hamcrest.CoreMatchers.endsWith
//import static org.hamcrest.CoreMatchers.equalTo
//import static org.hamcrest.CoreMatchers.startsWith
import static org.hamcrest.core.Is.is
import static org.junit.Assert.assertThat

@RunWith(SpringRunner)
@SpringBootTest
class PgSqlEmbeddedDemoApplicationTests {

    private EmbeddedPostgres postgres
    private String url

    @Before
    void setUp() throws Exception {
        postgres = new EmbeddedPostgres()
        url = postgres.start("localhost", 15433, "pgDataBase", "pgUser", "pgPassword", [])
    }

    @After
    void tearDown() throws Exception {
        postgres.stop()
    }

    @Test
    void ensurePostgresIsWorking() {
        final Connection conn = DriverManager.getConnection(url)
        assertThat(conn.createStatement().execute("CREATE TABLE films (code char(5));"), is(false))
        assertThat(conn.createStatement().execute("INSERT INTO films VALUES ('movie');"), is(false))
        final Statement statement = conn.createStatement()
        assertThat(statement.execute("SELECT * FROM films;"), is(true))
        assertThat(statement.getResultSet().next(), is(true))
        assertThat(statement.getResultSet().getString("code"), is("movie"))
    }

}
